package repository

import (
	"github.com/gin-gonic/gin"
	"yak.admin/src/entity"
	"yak.admin/src/repository"
	"yak.admin/src/service"
	"yak.admin/src/util"
)

type DiscussRepository struct {
	Page       int `form:"page"`
	Pagesize   int `form:"per-page"`
	Pid int `form:"Condition[pid]"`
	Province   string
	City       string
	County     string
	IsSuper    bool
	Status int `form:"Condition[status]"`
	YakLst     string `form:"Condition[yak_lst]"`
	YakLstTxt  string `form:"Condition[yak_lst_txt]"`
	YakLstExt  string `form:"Condition[yak_lst_ext]"`
	repository.ReportDiscussRepository
}

func (repository *DiscussRepository) FindAllCategory(c *gin.Context) ([]map[string]interface{}, int64) {
	if err := util.DefaultGetValidParams(c, repository); err != nil {
		return []map[string]interface{}{}, 0
	}

	reader := new(service.ReaderQuery).CreateModelQuery(&entity.ReportDiscussCategory{}).
		WithEQ("status", repository.Status).
		WithYakLst(repository.YakLst, repository.YakLstTxt, repository.YakLstExt).
		WithPCC(repository.Province, repository.City, repository.County, repository.IsSuper).
		WithPage(repository.Page).WithPageSize(repository.Pagesize).
		WithOrder("created_at", "desc")

	var discusses []*entity.ReportDiscussCategory
	reader.Reader(&discusses)
	statusLabels := map[int]string{1: "处理中", 2: "已完结"}
	res := reader.Datalist(discusses, len(discusses), func(index int, item interface{}) map[string]interface{} {
		m := item.(*entity.ReportDiscussCategory)
		return map[string]interface{}{
			"_serial":  reader.AsSerial(index),
			"pid": m.Pid,
			"comment": m.Comment,
			"status_label": statusLabels[m.Status],
			"status": m.Status,
			"count": m.Count,
		}
	})

	return res, reader.Count()
}

func (repository *DiscussRepository) FindAll(c *gin.Context) ([]map[string]interface{}, int64) {
	if err := util.DefaultGetValidParams(c, repository); err != nil {
		return []map[string]interface{}{}, 0
	}

	reader := new(service.ReaderQuery).CreateModelQuery(&entity.ReportDiscuss{}).
		WithEQ("pid", repository.Pid).
		WithPCC(repository.Province, repository.City, repository.County, repository.IsSuper).
		WithPage(repository.Page).WithPageSize(repository.Pagesize).
		// WithPreloads([]string{"ReportDiscussAnnex"}).
		WithOrder("created_at", "desc")

	var discusses []*entity.ReportDiscuss
	reader.Reader(&discusses)

	res := reader.Datalist(discusses, len(discusses), func(index int, item interface{}) map[string]interface{} {
		m := item.(*entity.ReportDiscuss)
		// annexes := m.ReportDiscussAnnex
		return map[string]interface{}{
			"_serial":  reader.AsSerial(index),
			"id": m.Id,
			"number": m.Number,
			"message": m.Message,
			"address": m.Address,
			// "imgs": reader.AsListField(annexes, len(annexes), "Src"),
		}
	})

	return res, reader.Count()
}

func (repository *DiscussRepository) FindDiscuss(id uint) map[string]interface{} {
	var discuss entity.ReportDiscuss
	reader := new(service.ReaderQuery).CreateModelQuery(&discuss).
		WithEQ("id", id).
		WithPreloads([]string{"ReportDiscussAnnex"})

	reader.One(&discuss)

	return reader.Viewlist(&discuss, func(item interface{}) map[string]interface{} {
		m := item.(*entity.ReportDiscuss)
		annexes := m.ReportDiscussAnnex
		return map[string]interface{}{
			"id": m.Id,
			"number": m.Number,
			"message": m.Message,
			"address": m.Address,
			"imgs": reader.AsListField(annexes, len(annexes), "Src"),
		}
	})
}
